<template>
	<view class="content">
		<bgyx-nav-bar name="消息">
			<view style="display: flex;align-items: center;">
				<zb-popover placement="bottom-end" :options="actions" ref="Popover" @select="csitemfunc"
					class="item-popover">
					<image src="/static/images/addmn.png" style="width: 50rpx;height: 50rpx;margin-right: 15rpx;"
						mode=""></image>
				</zb-popover>
			</view>
		</bgyx-nav-bar>

		<view class="indexcontent" @click="clkcon" style="">
			<view @click="gochat(item)"
				style="width: 100%;display: flex;align-items: flex-start;padding-bottom: 20rpx;padding-top: 10rpx;"
				v-for="(item,k) in list">
				<view style="position: relative;">
					<u-badge :offset="[-8,-8]" size="40" type="error"
						:count="item.count > 99 ? 99 : item.count"></u-badge>
					<u-avatar v-if="item.gid == 0" mode="square" size="90"
						:src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.fid + '.jpg'"></u-avatar>
					<bgyx-group-avatar :id="item.gid" v-else />
				</view>
				<view
					style="flex: 1;display: flex;justify-content: space-between;align-items: center;padding: 0rpx 20rpx 10rpx 20rpx;height: 100rpx;border-bottom: 1rpx solid #e1e1e155;">
					<view style="display: flex;flex-direction: column;flex: 1;">
						<view
							style="font-weight: bold;font-size: 30rpx;width: 100%;display: flex;justify-content: space-between;">
							<view v-if="item.status == '封禁' || item.status == 4" style="font-size: 32rpx;text-decoration: line-through;">
								{{item.name}}
							</view>
							<view v-else style="font-size: 32rpx;">
								{{item.name}}
							</view>
							<view v-if="item.status == '封禁' || item.status == 4" style="font-size: 30rpx;color: #b41515;font-weight: bold;">
								<text>封禁</text>
							</view>
							<view v-else style="font-size: 20rpx;color: #c6c6c6;font-weight: normal;">
								{{$u.timeFormat(item.lasttime, 'mm-dd hh:MM')}}
							</view>
						</view>
						<view style="font-size: 25rpx;color: #999;margin-top: 8rpx;width: 500rpx;" class="u-line-1">
							<bgyx-msg-display :info="item" />
						</view>
					</view>

				</view>
			</view>
		</view>



		<bgyx-tab-bar />
	</view>


</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		pub
	} from '../../common/event';
	export default {
		data() {
			return {
				content: '<span></span>',
				news: [],
				images: [],
				appname: '',
				name: 0,
				actions: [{
						text: '添加好友',
						icon: "/static/images/adduserw.png"
					},
					{
						text: '新建群聊',
						icon: "/static/images/addgroupw.png"
					},
					{
						text: '扫一扫',
						icon: "/static/images/scanw.png"
					},
				]
			}
		},
		components: {

		},
		computed: {
			...mapState({
				list: (state) => {
					return state.friend.list.filter(e => e.status != '拉黑').sort((a, b) => b.lasttime - a.lasttime)
				}
			})
		},
		watch: {
			list: function(v) {

			}
		},
		onShow() {
			if (this.$store.state.friend.list.length == 0) {
				this.$store.dispatch({
					type: "friend/init",
					payload: {}
				})
			}
		},
		onLoad() {
			this.getdata()
			// #ifdef H5
			this.actions = [{
					text: '添加好友',
					icon: "/static/images/adduserw.png"
				},
				{
					text: '新建群聊',
					icon: "/static/images/addgroupw.png"
				}
			]
			// #endif
			const user = uni.getStorageSync("user")
			if (!user || !user.id) {
				uni.redirectTo({
					url: "/pages/login/login"
				})
			}
		},
		methods: {
			getdata: function() {},
			csitemfunc: async function(e) {
				console.log(e)
				if (e.text == "添加好友") {
					uni.navigateTo({
						url: "/pages/friend/addfriend"
					})
				}

				if (e.text == "新建群聊") {
					uni.navigateTo({
						url: "/pages/friend/makegroup"
					})
				}

				if (e.text == "扫一扫") {
					this.scanewm()
				}
			},
			scanewm: function() {
				const _this = this
				uni.scanCode({
					success(res) {
						console.log(res)
						if (res.result) {
							if (res.result.includes('userewm_')) {
								const uid = res.result.replace('userewm_', '')
								const list = _this.$store.state.friend.list
								const user = _this.$store.state.user.user
								const ret = list.find(e => {
									return e.fid == uid
								})
								if (ret) {
									uni.navigateTo({
										url: "/pages/friend/detail?id=" + uid
									})
								} else {
									uni.navigateTo({
										url: "/pages/friend/friend?id=" + uid
									})
								}
							} else if (res.result.includes('groupewm_')) {
								const gid = res.result.replace('groupewm_', '')
								const list = _this.$store.state.friend.list
								const user = _this.$store.state.user.user
								const ret = list.find(e => {
									return e.gid == gid
								})
								if (ret) {
									uni.navigateTo({
										url: "/pages/friend/gdetail?id=" + gid
									})
								} else {
									uni.navigateTo({
										url: "/pages/friend/group?id=" + gid
									})
								}
							} else if (res.result.includes('http')) {
								plus.runtime.openWeb(res.result)
							}
						}
					}
				})
			},
			gologin: async function() {
				uni.switchTab({
					url: "/pages/friend/index"
				})
			},
			gonewinfo: async function(id) {
				if (this.appname == "BGYX_APP") {
					uni.navigateTo({
						url: "/pages/index/newinfo?id=" + id
					})
				}

			},
			clkcon: function() {
				this.$refs.Popover.close()
			},
			gochat: function(item) {
				if (item.status == '封禁' || item.status == 4) {
					uni.showToast({
						title: item.gid > 0 ? '群聊已封禁' : '用户已封禁',
						icon: "none"
					})
					return
				}
				this.$store.commit("friend/setCurrent", item)
				if (item.gid > 0) {
					uni.navigateTo({
						url: `/pages/friend/groupchat?id=${item.gid}&name=${item.name}&rid=${item.id}`
					})
					if (!this.$store.state.message.list[`g_${item.gid}`] || this.$store.state.message.list[
							`g_${item.gid}`].length == 0) {
						this.$store.dispatch({
							type: "message/initFmessage",
							payload: item
						})
					}
				} else {
					uni.navigateTo({
						url: `/pages/friend/friendchat?id=${item.fid}&name=${item.name}&rid=${item.id}`
					})
					if (!this.$store.state.message.list[`f_${item.fid}`] || this.$store.state.message.list[
							`f_${item.fid}`].length == 0) {
						this.$store.dispatch({
							type: "message/initFmessage",
							payload: item
						})
					}
				}

			},
		}
	}
</script>


<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
		padding-bottom: 120rpx;
		border-start-end-radius: 32rpx;
		border-start-start-radius: 32rpx;
	}
</style>